//
// Created by ezio on 19-11-29.
//

#include "cam_operation.h"

#if 1
int calc_status(stcamera_param buf_trans)//(char *buf)
{
    //注意， 基础((stcamera_param *)buf)->
//    stcamera_param * buf_trans = (stcamera_param *)buf;
    int ret = 0;
    if(buf_trans.snap == TRUE)
    {
        ret |= SNAP;
    }
    if(buf_trans.setparam == TRUE)
    {
        ret |= SET_PARAM;
    }
    if(buf_trans.rtsp == TRUE)
    {
        ret |= RTSP;
    }
    return ret;
}


void get_camera_param(stcamera_param * stcameraParam)
{
#ifdef MOCK
    memset(stcameraParam, 0 ,sizeof(stcamera_param));
#else
    // HI_MPI_ISP_GetExposureAttr(stcameraParam->ViPipe, stcameraParam->stExpAttr);

#endif
}
int configure_socket(int *fd,struct sockaddr_in *server_addr,struct sockaddr_in *client_addr,int *cln_sock)
{
    *fd = socket(AF_INET, SOCK_STREAM, 0);
    if(*fd == -1)
    {
        perror("socket");
        return -1;
    }

    server_addr->sin_family = AF_INET;
    server_addr->sin_port = htons(SERVER_PORT);
    //INADDR_ANY means set local machine addr
    server_addr->sin_addr.s_addr =INADDR_ANY;//inet_addr(SERVER_ADDRESS);
    bzero(server_addr->sin_zero,8);

    if(bind(*fd,(struct sockaddr*)server_addr,sizeof(struct sockaddr_in)) == -1)
    {
        perror("bind");
        return -1;
    }

    if(listen(*fd,5) == -1)
    {
        perror("listen");
        return -1;
    };

    socklen_t client_addr_size = sizeof(struct sockaddr_in);
    *cln_sock = accept(*fd,(struct sockaddr*)client_addr,&client_addr_size);
    if(*cln_sock < 0)
    {
        fprintf(stderr, "accep fail.\n" );
        return -1;
    }
    fprintf(stderr, "accep success.\n" );
    return 0;
}

int handshake_with_client(int cln_sock,fd_set myset,stcamera_param *stcameraParam_)
{

    int ret = -1;
    int recvbytes =0;
    char buff[MAX_BUFF_SIZE]={0};
    char send_buff[MAX_BUFF_SIZE]={0};




    ret = select(cln_sock+1, &myset, NULL, NULL, NULL);
    //可读
    if(ret>=0) {
        if (FD_ISSET(cln_sock, &myset)) {            
            if( (recvbytes = recv(cln_sock, stcameraParam_, sizeof(stcamera_param), 0)) == -1)
            {
                perror("recv");
                return -1;
            }
            //如果请求包正确
            fprintf(stderr,"%s %d Vipipe %d\n",__FUNCTION__,__LINE__,stcameraParam_->ViPipe);

        }
    }
    else{
        fprintf(stderr,"%s %d select err\n",__FUNCTION__,__LINE__);
        perror("select");
        return -1;
    }

/*move to main.c*/
    #if 0
    get_camera_param(stcameraParam_);
    const char* str_tmp = "cam_param";
    memcpy(send_buff,str_tmp,MAX_BUFF_SIZE);
    //send_buff为相机参数
    send(cln_sock, stcameraParam_, sizeof(stcamera_param), 0);
    ret = select(cln_sock+1, &myset, NULL, NULL, NULL);
    //可读
    if(ret>=0) {
        recv(cln_sock, buff, sizeof(stcamera_param), 0);
        //如果握手回复包正确，
        fprintf(stderr,"%s %d %s \n",__FUNCTION__,__LINE__,buff);

    }
    else {
        fprintf(stderr, "%s %d select err\n", __FUNCTION__, __LINE__);
        perror("select");
        return -1;
    }
    #endif
    return 0;
}
#endif


int tcp_send(int cln_sock, fd_set myset, BYTE* send_buff, int size)
{
    int ret = select(cln_sock+1, NULL, &myset,  NULL, NULL);
    //可读
    if(ret>=0) {     
        if( (ret = send(cln_sock, send_buff, size, 0)) == -1)
            {
                perror("recv");                
            }
            //如果请求包正确        
    }
    else 
    {
        perror("select");
    }
    
    return ret;   

    
}
int tcp_recv(int cln_sock, fd_set myset, BYTE* recv_buff, int size)
{
    int ret = select(cln_sock+1, &myset, NULL, NULL, NULL);
    //可读
    if(ret>=0) {     
        if( (ret = recv(cln_sock, recv_buff, size, 0)) == -1)
            {
                perror("recv");                
            }
            //如果请求包正确        
    }
    else 
    {
        perror("select");
    }
    
    return ret;
}
void wrap(BYTE* in_buff, int in_size, BYTE* out_buff, int out_size)
{
    //head
    int head_size =1;
    BYTE *head_buff = "a";
    int tail_size =1;
    BYTE *tail_buff = "b";

    memcpy(out_buff, head_buff, head_size);
    
    memcpy(out_buff+head_size, in_buff, in_size);

    memcpy(out_buff+head_size+in_size, tail_buff, tail_size);

    out_size = head_size+in_size+tail_size;
    
}